package com.b2bpsssm.controller.capital;

import com.b2bpsssm.common.R;
import com.b2bpsssm.pojo.Payment_Order;
import com.b2bpsssm.pojo.PurchasePassOrder;
import com.b2bpsssm.service.PaymentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@Api(value = "付款单数据测试",tags = "付款单API")
public class PaymentOrderController {
           @Autowired
        private PaymentOrderService paymentOrderService;

    // 查询付款单信息 有分页
    @RequestMapping(value = "/payment_order/queryPaymentOrderList",method = {RequestMethod.GET},produces = "application/json;charset=utf-8")
    @ApiOperation(value = "付款单信息 ",notes = "查询付款单信息 有分页",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
    })
    @ResponseBody
    public R queryPaymentOrderList(Integer page, Integer limit){
        // 调用Service层进行业务逻辑处理
        return paymentOrderService.queryPaymentOrderList(page,limit);
    }


    //根据单据编号查询付款单详情
    @RequestMapping(value = "/payment_order/queryPaymentOrderByNumber", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
    @ApiOperation(value = "付款单信息 ",notes = "根据单据编号查询付款单详情",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "document_no", value = "单据编号", required = true),

    })
    @ResponseBody
    public R queryPaymentOrderByNumber(String document_no){
        // 调用Service层进行业务逻辑处理
        return paymentOrderService.queryPaymentOrderByNumber(document_no);

    }
    //根据供货方查询付款单
    @RequestMapping(value = "/payment_order/queryPaymentOrderBySupplier", method = {RequestMethod.GET}, produces = "application/json;charset=utf-8")
    @ApiOperation(value = "付款单信息 ",notes = "根据供货方查询付款单",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier", value = "供货方", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
    })
    @ResponseBody
    public R queryPaymentOrderBySupplier(Integer page, Integer limit, String supplier){

        return paymentOrderService.queryPaymentOrderBySupplier(page,limit,supplier);

    }
    //    以供货商关键字 时间范围 进行关键字查询
    @RequestMapping(value = "/payment_order/selPaymentOrderLikeSupplier",method = RequestMethod.GET)
    @ApiOperation(value = "付款单信息 ",notes = "以供货商关键字 时间范围 进行关键字查询",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "supplier", value = "供货方", required = true),
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
            @ApiImplicitParam(name = "sub_date1", value = "开始时间", required = true),
            @ApiImplicitParam(name = "sub_date2", value = "结束时间", required = true),
    })
    @ResponseBody
    public R selPaymentOrderLikeSupplier(Integer page, Integer limit, String sub_date1, String sub_date2, String supplier) {
        return  paymentOrderService.selPaymentOrderLikeSupplier(page,limit,sub_date1,sub_date2,supplier);

    }
//    //上传附件
//    @RequestMapping(value = "/payment_order/accessory",method = RequestMethod.POST)
//    @ResponseBody
//    public R updatePaymentOrderAccessory(@Validated Payment_Order payment_order, HttpServletRequest request) throws IOException {
//        R r = new R();
//        if (payment_order.getPayment_order_accessory().isEmpty()) {
//            System.out.println("上传失败");
//            r.setCode(500);
//            r.setMassage("上传文件不能为空--上传失败");
//        } else {
//            System.out.println("开始上传");
//            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
//            File file = new File(uploadPath);
//            if (!file.exists()) {
//                file.mkdirs();
//            }
//            MultipartFile multipartFile = payment_order.getPayment_order_accessory();
//            String fileName = multipartFile.getOriginalFilename();
//            File newFile = new File(uploadPath + fileName);
//            System.out.println("newFile=" + newFile);
//            FileCopyUtils.copy(multipartFile.getBytes(), newFile);
//            r.setCode(200);
//            r.setMassage("上传成功");
//        }
//        return r;
//
//    }
        //添加付款单
        @RequestMapping(value = "/payment_order/addPaymentOrder", method = {RequestMethod.POST}, produces = "application/json;charset=utf-8")
        @ApiOperation(value = "付款单信息 ",notes = "添加付款单",httpMethod = "POST",response = R.class)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "supplier", value = "供货方", required = true),
                @ApiImplicitParam(name = "document_no", value = "单据编号", required = true),
                @ApiImplicitParam(name = "operator", value = "经手人", required = true),
                @ApiImplicitParam(name = "sub_date", value = "创建时间", required = true),
                @ApiImplicitParam(name = "clear_from", value = "结算时间", required = true),
                @ApiImplicitParam(name = "total_amount", value = "总计金额", required = true),
                @ApiImplicitParam(name = "discount_amount", value = "优惠金额", required = true),
                @ApiImplicitParam(name = "paid_amount", value = "实付金额", required = true),
                @ApiImplicitParam(name = "note", value = "备注", required = false),
        })
        @ResponseBody
        public R addPaymentOrder (@RequestBody PurchasePassOrder purchasePassOrder){

            return paymentOrderService.addPaymentOrder(purchasePassOrder);
        }

//


    }